package com.fulihui.information.service;

import com.fulihui.information.api.ContentCounterService;
import com.fulihui.information.biz.counter.ContentReadTimesOperableCounter;
import com.fulihui.information.biz.counter.ContentSharedTimesOperableCounter;
import com.fulihui.information.core.redis.RedisHotsContentReadTimesSortDB;
import com.fulihui.information.core.redis.RedisHotsContentSharedTimesSortDB;
import com.fulihui.information.model.UserContentOperate;
import com.fulihui.information.request.IncrContentCounterRequest;
import com.fulihui.integralbusiness.constants.IntegralBusinessFlags;
import com.fulihui.integralbusiness.producer.ZbusIntegralCreator;
import com.google.common.collect.Maps;
import org.near.servicesupport.result.TSingleResult;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import static com.fulihui.information.util.ContentScoreUtil.convReadTimesScore;
import static com.fulihui.information.util.ContentScoreUtil.convSharedTimesScore;
import static org.near.servicesupport.result.ResultBuilder.succTSingle;
import static org.near.toolkit.common.StringUtil.EMPTY_STRING;

/**
 * @author Willard.Hu on 2016/12/26 0026.
 */
@Service("contentCounterService")
public class ContentCounterServiceImpl implements ContentCounterService {
    @Autowired
    private ContentReadTimesOperableCounter   contentReadTimesOperableCounter;
    @Autowired
    private ContentSharedTimesOperableCounter contentSharedTimesOperableCounter;
    @Autowired
    private RedisHotsContentReadTimesSortDB   redisHotsContentReadTimesSortDB;
    @Autowired
    private RedisHotsContentSharedTimesSortDB redisHotsContentSharedTimesSortDB;
    @Autowired
    private ZbusIntegralCreator zbusIntegralCreator;

    /**
     * @see ContentCounterService#incrRead(IncrContentCounterRequest)
     */
    @Override
    public TSingleResult<Long> incrRead(IncrContentCounterRequest request) {
        UserContentOperate userContentOperate = new UserContentOperate();
        userContentOperate.setContentId(request.getContentId());
        redisHotsContentReadTimesSortDB.incrScore(request.getContentId(),
                convReadTimesScore(1), EMPTY_STRING);
        return succTSingle(contentReadTimesOperableCounter.operate(userContentOperate));
    }

    /**
     * @see ContentCounterService#incrUpVote(IncrContentCounterRequest)
     */
    @Override
    public TSingleResult<Long> incrUpVote(IncrContentCounterRequest request) {
        // TODO [huwei]
        return succTSingle(0L);
    }

    /**
     * @see ContentCounterService#incrDownVote(IncrContentCounterRequest)
     */
    @Override
    public TSingleResult<Long> incrDownVote(IncrContentCounterRequest request) {
        // TODO [huwei]
        return succTSingle(0L);
    }

    /**
     * @see ContentCounterService#incrShared(IncrContentCounterRequest)
     */
    @Override
    public TSingleResult<Long> incrShared(IncrContentCounterRequest request) {
        UserContentOperate userContentOperate = new UserContentOperate();
        userContentOperate.setContentId(request.getContentId());
        redisHotsContentSharedTimesSortDB.incrScore(request.getContentId(),
                convSharedTimesScore(1), EMPTY_STRING);
        Long operate = contentSharedTimesOperableCounter.operate(userContentOperate);

        if(StringUtil.isNotBlank(request.getUserId())) {
            try {
                Map<String, Object> msg = Maps.newHashMap();
                msg.put("userId", request.getUserId());
                msg.put("contentId", request.getContentId());
                zbusIntegralCreator.create(IntegralBusinessFlags.SHARE, msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return succTSingle(operate);
    }
}
